package com.bdqn.t296.noso.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdqn.t296.noso.entity.CCashier;
import com.bdqn.t296.noso.entity.CPharmacy;
import com.bdqn.t296.noso.entity.CReport;
import com.bdqn.t296.noso.entity.ReportVo;
import com.bdqn.t296.noso.service.PrescriService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 处方划价Controller
 */
@Controller
@RequestMapping("/prescri")
public class PrescriController {

    @Autowired
    PrescriService prescriService;

    @RequestMapping("/mobu")
    public Object mobu(Model model) {
        ReportVo reportVo = new ReportVo();
        model.addAttribute("reportVo", reportVo);
        return "hospital/ption";
    }

    //查询所有患者信息
    @RequestMapping("/mobuList")
    @ResponseBody
    public Object mobu(String durgname, ReportVo reportVo, int current) {
        reportVo.setReportName(durgname);
        //分页对象：查询获得 User 对象，查看第 1 页数据，每页显示 5 条记录
        Page<ReportVo> page = new Page<ReportVo>(current, 5);
        IPage<ReportVo> prescriPager = prescriService.mobu(page, reportVo);
        return prescriPager;
    }

    //查询用户有没有填写病因
    @RequestMapping("/selbing")
    @ResponseBody
    public Object selbing(Integer reid) {
        String selbing = prescriService.selbing(reid);
        return selbing;
    }

    //添加用户病因
    @RequestMapping("/addbing")
    @ResponseBody
    public Object addbing(Integer reid, String bing, CReport cReport) {
        cReport.setZhuan(bing);
        cReport.setReportId(reid);
        Integer addbing = prescriService.addbing(cReport);
        return addbing;
    }

    //查询药品所有信息
    @RequestMapping("/seldrug")
    @ResponseBody
    public Object seldrug(String pharmacyName, CPharmacy cPharmacy, @RequestParam(required = false, defaultValue = "1") int current) {
        cPharmacy.setPharmacyName(pharmacyName);
        Page<CPharmacy> page = new Page<CPharmacy>(current, 5);
        IPage<CPharmacy> cPharmacyIPage = prescriService.selpharm(page, cPharmacy);
        return cPharmacyIPage;
    }

    //查询处方中是否有这个药
    @RequestMapping("/selchu")
    @ResponseBody
    public Object seslchu(CCashier cCashier, Integer reid, String mename) {
        cCashier.setReportId(reid);//把用户id存入数据库
        cCashier.setDurgname(mename);//把药品名称存入实体类
        Integer selcadr = prescriService.selcadr(cCashier);
        return selcadr;
    }

    //添加处方药品
    @RequestMapping("/addchu")
    @ResponseBody
    public Object addchu(CCashier cCashier, CPharmacy cPharmacy) {
        //向处方添加药品
        Integer addchu = prescriService.addchu(cCashier);
        String pharmacyName = cCashier.getDurgname();
        cPharmacy.setPharmacyName(pharmacyName);
        //减少药房中的数量
        Integer deldrunum = prescriService.deldrunum(cPharmacy);
        return addchu;
    }

    //删除处方中的药品
    @RequestMapping("/del")
    @ResponseBody
    public Object del(Integer cashier, String durnme, Integer durnum, CPharmacy cPharmacy) {
        Integer del = prescriService.del(cashier);//删除
        cPharmacy.setPharmacyName(durnme);
        cPharmacy.setDrugstorenum(durnum);
        prescriService.adddrunum(cPharmacy);//添加药品数量
        if (del == 0) {
            return "删除失败";
        } else {
            return "删除成功";
        }
    }

    //如果处方中有该药品则修改该药品的数量和价钱
    @RequestMapping("/updchu")
    @ResponseBody
    public Object updchu(CCashier cCashier, CPharmacy cPharmacy) {
        //修改处方中药品的数量
        Integer updchu = prescriService.updchu(cCashier);
        String pharmacyName = cCashier.getDurgname();
        cPharmacy.setPharmacyName(pharmacyName);
        //修改仓库中药品的数量
        Integer deldrunum = prescriService.deldrunum(cPharmacy);
        return updchu;
    }

    //查看用户的检查结果
    @RequestMapping("/lookbing")
    @ResponseBody
    public Object lookbing(Integer reid) {
        String lookbing = prescriService.lookbing(reid);
        return lookbing;
    }

    //查看该用户是否还有未交钱的项目
    @RequestMapping("/seljiao")
    @ResponseBody()
    public Object seljiao(Integer reid) {
        int seljiao = prescriService.seljiao(reid);
        return seljiao;
    }

    //查询该用户的药品处方
    @RequestMapping("/selpepi")
    @ResponseBody
    public Object selpepi(Integer reportId, int current) {
        Page<CCashier> page = new Page<CCashier>(current, 5);
        IPage<CCashier> cCashierList = prescriService.selpepi(page, reportId);
        return cCashierList;
    }

    //查询该用户所有的处方
    @RequestMapping("/selall")
    @ResponseBody
    public Object selall(Integer reportId, int current) {
        //分页对象：查询获得 User 对象，查看第 1 页数据，每页显示 5 条记录
        Page<CCashier> page = new Page<CCashier>(current, 5);
        IPage<CCashier> cCashierList = prescriService.selall(page, reportId);
        return cCashierList;
    }

    //查询用户所有的项目处方
    @RequestMapping("/selximu")
    @ResponseBody
    public Object selximu(Integer reportId, int current) {
        Page<CCashier> page = new Page<CCashier>(current, 5);
        IPage<CCashier> cCashierService = prescriService.selximu(page, reportId);
        return cCashierService;
    }

    //查看该用户是否有缴费未做的项目
    @RequestMapping("/selwei")
    @ResponseBody
    public Object selwei(Integer reid) {

        //查询该用户有几个做过的项目
        Integer selyi = prescriService.selyi(reid);
        //查询该用户有几个缴费的项目
        Integer selgong = prescriService.selgong(reid);
        System.out.println("受没有=" + selyi + "---" + selgong);
        if (selyi == selgong) {
            return 1;
        } else {
            return 0;
        }
    }
}